package com.cqxyd.smartsite.controller.vs1;

import com.cqxyd.smartsite.pojo.*;
import com.cqxyd.smartsite.service.DeviceInfoService;
import com.cqxyd.smartsite.service.ProgramInfoService;
import com.cqxyd.smartsite.service.UploadRecordService;
import com.cqxyd.smartsite.util.GetHelper;
import com.cqxyd.smartsite.util.JSONResult;
import com.cqxyd.smartsite.util.PostHelper;
import com.cqxyd.smartsite.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@ApiVersion(1)
@RequestMapping(value = "{version}/api/attendance")
@Api(value = "考勤数据信息接口", tags = "考勤数据信息接口")
public class AttendanceController {
    private final Logger logger = LoggerFactory.getLogger(AttendanceController.class);
    @Autowired
    private PostHelper postHelper;
    @Autowired
    private GetHelper getHelper;
    @Autowired
    private UploadRecordService uploadRecordService;
    @Autowired
    private ProgramInfoService programInfoService;

    @PostMapping(value = "download")
    @ApiOperation(value = "下载人员信息", notes = "下载人员信息", produces = "application/json")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int"),
                    @ApiImplicitParam(name = "projectId", value = "项目编码", required = true, paramType = "query", dataType = "int")
            })
    public JSONResult<String> postHeartbeat(@PathVariable("version") String version, Integer page, Integer projectId) {
        try {
            ProgramInfo rrogramInfo = programInfoService.selectOne(projectId);
            return getHelper.downLoadProjectWorker(rrogramInfo.getProgramKey(), rrogramInfo.getProgramSecret(), page);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return JSONResult.errorMsg("下载人员信息失败:" + e.getMessage());
        }
    }

    @PostMapping(value = "record")
    @ApiOperation(value = "上传人员考勤信息", notes = "上传人员考勤信息", produces = "application/json")
    public JSONResult<String> postAttendance(@PathVariable("version") String version) {
        try {
            return postHelper.postAttendanceRecord();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return JSONResult.errorMsg("上传考勤信息失败:" + e.getMessage());
        }
    }

}
